package com.example.mobile2021606128wje0507;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radgroup;
    private RadioButton rdbtnMainYingqing;
    private RadioButton rdbtnMainQianboli;
    private RadioButton rdbtnMainLuntai;
    private RadioButton rdbtnMainDipan;
    private RadioButton rdbtnQiangai;
    private Button btnMainAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
        btnMainAdd.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             int radBtnId =radgroup.getCheckedRadioButtonId();
             if (radBtnId!=-1){
                 RadioButton radioButton=findViewById( radBtnId );
                 Intent intent =new Intent();
                 intent.putExtra( "part",radioButton.getText().toString());
                 setResult( RESULT_OK,intent );
                 finish();
             }else {
                 new AlertDialog.Builder(MainActivity.this)
                         .setMessage( "请选择原材料" )
                         .setPositiveButton( "确定",null )
                         .create()
                         .show();
             }
            }
        } );
    }

    private void initView() {
        radgroup = (RadioGroup) findViewById( R.id.radgroup );
        rdbtnMainYingqing = (RadioButton) findViewById( R.id.rdbtnMainYingqing );
        rdbtnMainQianboli = (RadioButton) findViewById( R.id.rdbtnMainQianboli );
        rdbtnMainLuntai = (RadioButton) findViewById( R.id.rdbtnMainLuntai );
        rdbtnMainDipan = (RadioButton) findViewById( R.id.rdbtnMainDipan );
        rdbtnQiangai = (RadioButton) findViewById( R.id.rdbtnQiangai );
        btnMainAdd = (Button) findViewById( R.id.btnMainAdd );
    }
}